package com.example.demo2_review.changYongJiHe;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author zwp14
 * @create 2024/6/17 16:59
 * @description 集合遍历的方法
 */
public class Demo2ListForForeachMethod {

    public static void main(String[] args) {
        List<String> forList = new ArrayList<>();
        forList.add("11");
        forList.add("12");
        forList.add("13");
        forList.add("14");
        forList.add("15");
        //普通for遍历
        listForMethod(forList);
        //增强for遍历
        listForStrongMethod(forList);
        //foreach遍历
        listForeachMethod(forList);
        //迭代器遍历Iterator
        listIteratorMethod(forList);
        //Stream遍历
        listStreamMethod(forList);
    }

    /**
     * Stream遍历
     * @param forList 集合
     */
    private static void listStreamMethod(List<String> forList) {
        forList.stream().forEach(s -> {
            int i = forList.indexOf(s);
            System.out.println("streamList"+i+"="+s);
        });
    }

    /**
     * 迭代器遍历
     * @param forList 集合
     */
    private static void listIteratorMethod(List<String> forList) {
        Iterator<String> iterator = forList.iterator();
        /*
        * iterator.forEachRemaining(System.out::println);已经消耗了所有的元素，
        * 所以在之后的 while (iterator.hasNext()) 循环中没有元素可供迭代器获取，
        * 因此 System.out.println("next = " + next); 不会执行。
        * 反过来，while (iterator.hasNext())在前，iterator.forEachRemaining(System.out::println)在后
        * 亦是如此
         */
        while (iterator.hasNext()){
            String next = iterator.next();
            int i = forList.indexOf(next);
            System.out.println("iteratorList"+i+" = " + next);
        }
        iterator.forEachRemaining(s -> {
            int i = forList.indexOf(s);
            System.out.println("iteratorList"+i+" = " + s);
        });

        System.out.println("-------------------------------------");
    }

    /**
     * foreach遍历
     * @param forList 集合
     */
    private static void listForeachMethod(List<String> forList) {
        forList.forEach(s -> {
            int i = forList.indexOf(s);
            System.out.println("foreachList" + i + " = " + s);
        });
        System.out.println("-------------------------------------");
    }

    /**
     * 增强for遍历
     * @param forList 集合
     */
    private static void listForStrongMethod(List<String> forList) {

        for (String s : forList) {
            int i = forList.indexOf(s);
            System.out.println("forStrongList" + i + "=" + s);
        }
        System.out.println("-------------------------------------");
    }

    /**
     * 普通for遍历
     * @param forList 集合
     */
    private static void listForMethod(List<String> forList) {
        int size = forList.size();
        for (int i = 0; i < size; i++) {
            System.out.println("forList" + i + "=" + forList.get(i));
        }

        for (int i = forList.size() - 1; i >= 0; i--) {
            System.out.println("forList" + i + " = " + forList.get(i));
        }
        System.out.println("-------------------------------------");
    }
}
